package eu.testing.system.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

import eu.testing.system.models.Question;
import eu.testing.system.models.Variant;
import eu.testing.system.controllers.SystemController;
import eu.testing.system.dao.QuestionDao;
import eu.testing.system.dao.VariantDao;

public class QuestionDaoImpl extends SimpleJdbcDaoSupport implements
		QuestionDao {

	private static final String SQL_SELECT = "SELECT * FROM question WHERE TEST_ID = ?";
	private static final String SQL_DELETE = "DELETE FROM question WHERE TEST_ID = ?";
	private static final String SQL_UPDATE = "UPDATE question SET VALUE = ? WHERE TEST_ID = ? AND QUESTION_ID = ?";
	private static final String SQL_INSERT = "INSERT INTO question (TEST_ID, QUESTION_ID, VALUE) VALUES (?, ?, ?)";
	SystemController controller = new SystemController();

	@Override
	public Question getQuestion(int questionId) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Question> getAllQuestionsById(int testId) {
		@SuppressWarnings("deprecation")
		List<Question> questions = getSimpleJdbcTemplate().query(SQL_SELECT,
				new QuestionParameterizedRowMapper(), testId);

		for (int i = 0; i < questions.size(); i++) {
			Question question = questions.get(i);

			List<Variant> variants = controller.variantDao.getAllVariantsById(
					question.getTestId(), question.getQuestionId());
			question.setVariants(variants);
			questions.set(i, question);
		}
		return questions;
	}

	@Override
	public void insertQuestion(Question question) {
		getSimpleJdbcTemplate().update(SQL_INSERT, question.getTestId(),
				question.getQuestionId(), question.getQuestionValue());
		for (int i = 0; i < question.getVariants().size(); i++) {
			Variant variant = question.getVariants().get(i);
			variant.setTestId(question.getTestId());
			variant.setQuestionId(question.getQuestionId());
			variant.setVariantId(i);
			controller.variantDao.insertVariant(variant);

		}

	}

	@Override
	public void deleteQuestion(int testId) {
		getJdbcTemplate().update(SQL_DELETE, testId);

		controller.variantDao.deleteVariant(testId);

	}

	public class QuestionParameterizedRowMapper implements
			ParameterizedRowMapper<Question> {
		@Override
		public Question mapRow(ResultSet rs, int rowNum) throws SQLException {
			Question question = new Question();
			question.setTestId(rs.getInt("TEST_ID"));
			question.setQuestionId(rs.getInt("QUESTION_ID"));
			question.setQuestionValue(rs.getString("VALUE"));

			return question;
		}

	}

	@Override
	public void editQuestion(Question question) {
		getSimpleJdbcTemplate().update(SQL_UPDATE, question.getQuestionValue(),
				question.getTestId(), question.getQuestionId());
		for (int i = 0; i < question.getVariants().size(); i++) {
			Variant variant = question.getVariants().get(i);
			variant.setTestId(question.getTestId());
			variant.setQuestionId(question.getQuestionId());
			variant.setVariantId(i);
			controller.variantDao.editVariant(variant);
		}

	}

}
